#import "LSLHSBColorPicker.h"
///////////////////////////////////////
/**       LSLColorCicle七彩色环       */
///////////////////////////////////////
@interface LSLColorCicle : UIView

@end

@implementation LSLColorCicle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {//获取父类的参数
        self.backgroundColor = [UIColor greenColor];
        self.layer.cornerRadius = frame.size.width * 0.5;//将图层设置为圆角 再设置view的颜色就被遮住
        self.layer.masksToBounds = YES;//隐藏边界
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.layer.cornerRadius = self.frame.size.width * 0.5;
}

- (void)drawRect:(CGRect)rect
{
    CGPoint center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    CGFloat radius = self.frame.size.width;
    for (int hue = 0; hue <= 360; hue++) {
        CGFloat secA = (hue) / 180.0 * M_PI + M_PI;
        CGFloat a = radius * sin(secA);
        CGFloat b = radius * cos(secA);
        CGPoint toPoint = CGPointMake(b + center.x, a + center.y);
        UIBezierPath *path = [UIBezierPath bezierPath];
        path.lineWidth = 5;
        [path moveToPoint:center];
        [path addLineToPoint:toPoint];
        [path stroke];
        [[UIColor colorWithHue:(1.0 * hue / 360) saturation:1.0 brightness:1.0 alpha:1.0] set];
    }
}
@end
///////////////////////////////////////
/**       LSLCenterCircleView        */
///////////////////////////////////////
@interface LSLCenterCircleView : UIView

@property (nonatomic, strong) UIView *colorView;
@property (nonatomic, strong) UIColor *centerCircleColor;

@end

@implementation LSLCenterCircleView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _centerCircleColor = [UIColor redColor];
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:_colorView];
    }
    return self;
}

- (void)setCenterCircleColor:(UIColor *)centerCircleColor {
    _centerCircleColor = centerCircleColor;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, self.centerCircleColor.CGColor);
    CGRect frame = CGRectMake(5,5,rect.size.width-10,rect.size.height -10);
    CGContextAddEllipseInRect(context, frame);
    CGContextFillPath(context);
}
@end
///////////////////////////////////////
/**   LSLDripView  指示器滑块          */
///////////////////////////////////////
@interface LSLDripView : UIView

@property (nonatomic, strong) UIView *dripView;
@property (nonatomic, strong) UIColor *dripCircleColor;

@end

@implementation LSLDripView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _dripCircleColor = [UIColor redColor];
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:_dripView];
    }
    return self;
}

- (void)setDripCircleColor:(UIColor *)dripCircleColor {
    _dripCircleColor = dripCircleColor;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, self.dripCircleColor.CGColor);
    CGRect frame = CGRectMake(0,0,rect.size.width,rect.size.height );
    CGContextAddEllipseInRect(context, frame);
    CGContextAddLineToPoint(context, 0, 0);
    CGContextFillPath(context);
}
@end
///////////////////////////////////////
/**          白色遮罩层。           *////
///////////////////////////////////////
@interface DrawWhiteCyclesView : UIView
@end

@implementation DrawWhiteCyclesView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect frame = CGRectMake(0,0,rect.size.width,rect.size.height );
    [[UIColor colorWithWhite:0 alpha:0] set];
    CGContextFillRect(ctx, rect);
    CGContextAddEllipseInRect(ctx, frame);
    [[UIColor whiteColor] set];
    CGContextFillPath(ctx);
}
@end
///////////////////////////////////////
/**                 连接线         *////
///////////////////////////////////////
@interface DrawLineView : UIView

@property (nonatomic, strong) UIView *LineView;
@property (nonatomic, assign) CGPoint dripViewCenter;
@property (nonatomic, assign) CGPoint colorCircleViewCenter;

@end

@implementation DrawLineView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setDripViewCenter:(CGPoint)dripViewCenter{
    _dripViewCenter = dripViewCenter;
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGMutablePathRef path =  CGPathCreateMutable();
    CGContextMoveToPoint(ctx, self.dripViewCenter.x, self.dripViewCenter.y);
    CGContextAddLineToPoint(ctx,self.colorCircleViewCenter.x, self.colorCircleViewCenter.y);
    CGContextAddPath(ctx, path);
    CGContextStrokePath(ctx);
}
@end

//////////////////////////////////////////////////
/**             LSLHSBColorPickerView           */
//////////////////////////////////////////////////
typedef void(^ColorSelectedBlock)(UIColor *, BOOL);

@interface LSLHSBColorPicker ()
@property (nonatomic, copy) ColorSelectedBlock colorSelectedBlock;
@property (nonatomic, getter=isConfirm) BOOL confirm;


@property (nonatomic, strong) UIColor *color;
@property (nonatomic, assign) CGFloat hue;
@property (nonatomic, assign) CGFloat saturation;
@property (nonatomic, assign) CGFloat brightness;
@property (nonatomic, assign) CGFloat alpha;
@property (nonatomic, strong) UIView *colorCircleView;
@property (nonatomic, strong) DrawWhiteCyclesView *whiteCyclesView;
@property (nonatomic, strong) LSLCenterCircleView *centerCircleView;
@property (nonatomic, strong) LSLColorCicle *colorView;//色环
@property (nonatomic, strong) UIButton *dripBtn;
@property (nonatomic, strong) LSLDripView *dripView;
@property (nonatomic, assign) CGFloat radius;
@property (nonatomic, assign) CGFloat dripViewX;
@property (nonatomic, strong) DrawLineView *lineView;
@property (nonatomic, assign) CGPoint dripViewCenter;

@end

static CGFloat DripViewWidthAndHeight = 30;//指示器滑块的直径

@implementation LSLHSBColorPicker

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    _hue = 0.0;//色调
    _saturation = 1.0;//饱和度
    _brightness = 1.0;//亮度
    _alpha = 1.0;//透明度
    
    _colorCircleView = [[UIView alloc] init];//放置色环的视图
    
    _colorView = [[LSLColorCicle alloc] init];//色环的外接圆角矩形
    [_colorCircleView addSubview:_colorView];//调用layoutSubviews
    
     _whiteCyclesView = [[DrawWhiteCyclesView alloc]init];
    [_colorView addSubview:_whiteCyclesView];
    
     _lineView = [[DrawLineView alloc] init];
    [_colorCircleView addSubview:_lineView];//
    
    _centerCircleView = [[LSLCenterCircleView alloc] init];
    [_colorCircleView addSubview:_centerCircleView];
    
    _dripView  = [[LSLDripView alloc] init];
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(changedPositionAndColor:)];
    [_dripView addGestureRecognizer:panRecognizer];
    [_colorCircleView addSubview:_dripView];
    
    _dripBtn = [[UIButton alloc] initWithFrame:_dripView.bounds];
    [_dripView addSubview:_dripBtn];
    [_dripView bringSubviewToFront:_dripBtn];//将Btn显示在最前面
    [self addSubview:_colorCircleView];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGSize size              = self.frame.size;
    CGFloat square           = fmin(size.height, size.width);//获取短的一边（width）
    size                     = CGSizeMake(square - 20, square -20);//放置色环的视图//以短边形成的正方形
    CGFloat x                = (self.frame.size.width - size.width) * 0.5; //等于0
    _radius                  = size.width * 0.5;//短边的一半
    _dripViewX               = _radius - DripViewWidthAndHeight * 0.7;//指示器滑块的轨道半径
    CGFloat radiusCenter     = (size.width - 24) *0.25;//中心小圆的直径
    _colorCircleView.frame   = CGRectMake(x, x, size.width, size.height);//放置色环的视图
    _colorView.frame         = CGRectMake(12, 12, size.width - 24, size.height - 24); //色环
    CGFloat whiteDiameter    = size.width - DripViewWidthAndHeight * 2  ;//空白圆的直径
    _whiteCyclesView.frame   = CGRectMake(0, 0, whiteDiameter, whiteDiameter);
    _whiteCyclesView.center  = CGPointMake(_radius - 12, _radius - 12);
    _lineView.frame          = CGRectMake(0, 0, size.width, size.height);
    _centerCircleView.frame  = CGRectMake(0,0 , radiusCenter , radiusCenter);
    _centerCircleView.center = CGPointMake(_radius, _radius);
    _dripView.frame          = CGRectMake(_dripViewX, 0, DripViewWidthAndHeight, DripViewWidthAndHeight);
     _dripBtn.frame          = _dripView.bounds;
    self.lineView.dripViewCenter        = [self changeDripViewPositionWithDuration];
    self.lineView.colorCircleViewCenter = _centerCircleView.center;
}

- (void)changedPositionAndColor:(UIGestureRecognizer *)recognizer{
    CGPoint point = [recognizer locationInView:_colorCircleView];
    CGFloat dx = point.x - self.radius;
    CGFloat dy = point.y - self.radius;
    CGFloat angle = atan2f(dy, dx);
    if (dy != 0) {
        angle += M_PI;
        _hue = angle / (2 * M_PI);
    } else if (dx > 0) {
        _hue = 0.5;
    }
    [self changeValue];
}

- (UIColor *)changeColor{
    _color = [UIColor colorWithHue:_hue saturation:_saturation brightness:_brightness alpha:_alpha];
    self.centerCircleView.centerCircleColor = _color;
    self.dripView.dripCircleColor = _color;
    [self revertSelectedColorByBlock];

    return _color;
}

- (void)changeValue{
    //change color and dripcenter
    _dripViewCenter = [self changeDripViewPositionWithDuration];
    [self changeColor];
    self.lineView.dripViewCenter = _dripViewCenter;
}

- (CGPoint)changeDripViewPositionWithDuration{
    CGFloat currentAngle = M_PI * 2 * _hue - M_PI;
    CGFloat cx           = self.dripViewX * cos(currentAngle) + self.dripViewX +6;
    CGFloat cy           = self.dripViewX * sin(currentAngle) + self.dripViewX +6;
    CGRect frame         = self.dripView.frame;
    frame.origin.x       = cx;
    frame.origin.y       = cy;
    self.dripView.frame  = frame;
    // dripView shadow
    CGFloat offsetX      = 2 * cos(currentAngle);
    CGFloat offsetY      = 2 * sin(currentAngle + M_PI);
    self.dripView.layer.shadowOffset  = CGSizeMake(offsetX, offsetY);
    self.dripView.layer.shadowRadius  = 4;
    self.dripView.layer.shadowOpacity = 0.2;
    return self.dripView.center;
}


#pragma mark - block

- (void)colorSelectedBlock:(void (^)(UIColor *, BOOL))colorSelectedBlock {
    if (colorSelectedBlock) {
        self.colorSelectedBlock = colorSelectedBlock;
    }
}

- (void)revertSelectedColorByBlock {
    if (self.colorSelectedBlock) {
        self.colorSelectedBlock(self.color, self.isConfirm);
    }
}

@end
